package com.aaa.doctor.service.impl;


import com.aaa.doctor.dao.CareHistoryDao;
import com.aaa.doctor.dao.CareOrderDao;
import com.aaa.doctor.dao.CareOrderItemDao;
import com.aaa.doctor.feign.DeptFeign;
import com.aaa.doctor.feign.UserFeign;
import com.aaa.doctor.service.CareHistoryService;
import com.aaa.doctor.utils.IdWorker;
import com.aaa.entity.CareHistory;
import com.aaa.entity.CareOrder;
import com.aaa.entity.CareOrderItem;
import com.aaa.entity.User;
import com.aaa.utils.JWTUtils;
import com.aaa.utils.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @version 1.8.0_181
 * @Author ZXJ
 * @Date 2023/3/24 22:54
 * @注释
 */
@Service
public class CareHistoryServiceImpl implements CareHistoryService {
    @Resource
    private CareHistoryDao careHistoryDao;
    @Resource
    private CareOrderDao careOrderDao;
    @Resource
    private CareOrderItemDao careOrderItemDao;
    @Override
    public Result<List<CareHistory>> getPatientAllMessageByPatientId(String patientId) {

        QueryWrapper<CareHistory> wrapper = new QueryWrapper<>();
        if (Objects.nonNull(patientId)){
            wrapper.eq("patient_id",patientId);
            List<CareHistory> careHistories = careHistoryDao.selectList(wrapper);
            for (CareHistory careHistory : careHistories) {
                QueryWrapper<CareOrder> wrapper1 = new QueryWrapper<>();
                if (Objects.nonNull(careHistory.getChId()) ){
                    wrapper1.eq("ch_id",careHistory.getChId());
                    List<CareOrder> careOrders = careOrderDao.selectList(wrapper1);
                    careHistory.setCareOrderList(careOrders);
                    for (CareOrder careOrder : careOrders) {
                        QueryWrapper<CareOrderItem> wrapper2 = new QueryWrapper<>();
                        if (Objects.nonNull(careOrder.getCoId())){
                            wrapper2.eq("co_id",careOrder.getCoId());
                            List<CareOrderItem> careOrderItems = careOrderItemDao.selectList(wrapper2);
                            careOrder.setCareOrderItemList(careOrderItems);
                        }
                    }
                }
            }
            return new Result<>(200,"查询成功",careHistories);
        }
        return null;

    }

    @Override
    public Result<List<CareHistory>> getCareHistoryByRegId(String registrationId) {
        List<CareHistory> careHistoryByRegId = careHistoryDao.getCareHistoryByRegId(registrationId);
        return new Result<>(200,"查询处方成功",careHistoryByRegId);
    }

    @Resource
    private UserFeign userFeign;
    @Resource
    private DeptFeign deptFeign;
    @Override
    public Result<CareHistory> handleSaveCareHistory(String registrationId,CareHistory careHistory) {

        QueryWrapper<CareHistory> wrapper5 = new QueryWrapper<>();
        wrapper5.eq("reg_id", registrationId);
        CareHistory careHistory2 = careHistoryDao.selectOne(wrapper5);
        HttpServletRequest request = WebUtil.getRequest();
        String phone = (String) JWTUtils.getInfo(request.getHeader("token")).get("username");
        User byUsername = userFeign.getByUsername(phone);
        QueryWrapper<CareHistory> wrapper = new QueryWrapper<>();
        wrapper.eq("registrationId",registrationId);
        careHistory.setUserId(byUsername.getUserId());
        careHistory.setUserName(byUsername.getUserName());
        careHistory.setDeptId(byUsername.getDeptId());
        String deptName = deptFeign.getDeptById(byUsername.getDeptId()).getDeptName();
        careHistory.setDeptName(deptName);
        careHistory.setRegId(registrationId);

        //careHistory.setDeptName(byUsername);
        LocalDateTime now = LocalDateTime.now();
        careHistory.setCareTime(now);
        Date date = new Date();//格式日期（String）
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String caretime = simpleDateFormat.format(date);
        //careHistory.setCaseDate(caretime);
        if (careHistory2 == null) {
            IdWorker worker = new IdWorker();
            long chId = worker.nextId();
            String str="CH"+chId;
            careHistory.setChId(str);
            careHistoryDao.insert(careHistory);
            CareHistory careHistory1 = careHistoryDao.selectById(careHistory);
            return new Result(200,"添加成功",careHistory.getChId());
        }else {
            wrapper5.eq("ch_id",careHistory2.getChId());
            int i = careHistoryDao.update(careHistory,wrapper5);
            return new Result(200, "修改成功",careHistory.getChId());
        }


    }

    @Override
    public Result zairu(String patientId) {
        QueryWrapper<CareHistory> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(patientId)){
            wrapper.eq("patient_id",patientId);
            wrapper.orderByDesc("care_time");
        }
        CareHistory careHistory = careHistoryDao.selectList(wrapper).get(0);
        QueryWrapper<CareOrder> wrapper1 = new QueryWrapper<>();
        if (StringUtils.hasText(careHistory.getChId())){
            wrapper1.eq("ch_id",careHistory.getChId());
            List<CareOrder> careOrders = careOrderDao.selectList(wrapper1);
            careHistory.setCareOrderList(careOrders);
            for (CareOrder careOrder : careOrders) {
                QueryWrapper<CareOrderItem> careOrderItemQueryWrapper = new QueryWrapper<>();
                if (StringUtils.hasText(careOrder.getCoId())){
                    careOrderItemQueryWrapper.eq("co_id",careOrder.getCoId());
                    List<CareOrderItem> careOrderItems = careOrderItemDao.selectList(careOrderItemQueryWrapper);
                    careOrder.setCareOrderItemList(careOrderItems);
                }
            }
        }
        //careHistory
        return new Result<>(200,"成功",careHistory) ;
    }
}
